package com.wbt.bambootown.ui.activity;


import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.gyf.barlibrary.ImmersionBar;
import com.wbt.bambootown.R;
import com.wbt.bambootown.cons.Cons;
import com.wbt.bambootown.entity.DetailData;
import com.wbt.bambootown.mvp.contract.IconDataContract;
import com.wbt.bambootown.mvp.frame.MvpActivity;
import com.wbt.bambootown.mvp.frame.base.BaseView;
import com.wbt.bambootown.mvp.presenter.IconDataPresenter;
import com.wbt.bambootown.utils.ActivityManagerUtil;
import com.wbt.bambootown.utils.EventCenter;
import com.wbt.bambootown.utils.SPUtils;
import com.wbt.bambootown.utils.chart.ChartUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class IconDataActivity extends MvpActivity<IconDataPresenter> implements IconDataContract.View {
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_date)
    TextView mTvTime;
    @BindView(R.id.lineChart)
    LineChart lineChart;

    private List<String> xDataList;
    private List<Entry> yDataList;

    private String name;

    @OnClick(R.id.iv_back)
    public void onClick(View view){
        ActivityManagerUtil.create().finishActivity();
    }


    @Override
    public void showError(String errorMsg) {
        Toast.makeText(this , errorMsg , Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showDataSuccess(List<DetailData> detailDatas) {
        for (int i = 0; i < detailDatas.size(); i++) {
            xDataList.add(detailDatas.get(i).getTime());
            yDataList.add(new Entry(Float.parseFloat(detailDatas.get(i).getValue()) , i));
        }
        ChartUtil.showChart(this, lineChart, xDataList, yDataList, name + "趋势图", "数值",detailDatas.get(0).getUnit());
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initStatusBar(ImmersionBar immersionBar) {
        immersionBar
                .fitsSystemWindows(true)
                .statusBarColor(R.color.theme_color)
                .init();
    }

    @Override
    public void initView() {
        int id = getIntent().getIntExtra("id" , -1);
        name = getIntent().getStringExtra("name");

        xDataList = new ArrayList<>();
        yDataList = new ArrayList<>();

        mTvName.setText(name);
        mTvTime.setText(SPUtils.getString(Cons.DateSerach.BEGINTIME).split(" ")[0] + " 至 " + SPUtils.getString(Cons.DateSerach.ENDTIME).split(" ")[0]);
        mPresenter.getHistoryData(id , SPUtils.getString(Cons.DateSerach.BEGINTIME) , SPUtils.getString(Cons.DateSerach.ENDTIME));

    }

    @Override
    public void initData() {

    }

    @Override
    public Object getContentView() {
        return R.layout.activity_icon_data;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }
}
